package com.sh3h.generator.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.sh3h.generator.web.domain.GenTable;
import com.sh3h.generator.web.domain.GenTableField;
import com.sh3h.generator.web.dtos.GenTableDto;
import com.sh3h.generator.web.dtos.Result;
import com.sh3h.generator.web.service.impl.GenTableFieldServiceImpl;
import com.sh3h.generator.web.service.impl.GenTableServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.rmi.ServerException;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping({"/api/gen/tables"})
public class TableController {
    @Autowired
    private GenTableServiceImpl genTableService;
    @Autowired
    private GenTableFieldServiceImpl genTableFieldService;

    @GetMapping("")
    public Result getByPage(int pageNum, int pageSize, String tableName, String packageName) {
        Page<GenTable> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<GenTable> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(tableName)) {
            queryWrapper.eq(GenTable::getTableName, tableName);
        }
        if (!StringUtils.isNullOrEmpty(packageName)) {
            queryWrapper.eq(GenTable::getPackageName, packageName);
        }
        Page<GenTable> datasourceList = genTableService.page(page, queryWrapper);
        return Result.success(datasourceList);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") String id) {
        GenTable datasource = genTableService.getById(id);
        return Result.success(datasource);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") String id) {
        Boolean result = genTableService.removeById(id);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    public Result updateById(@RequestBody GenTableDto dto) {
        GenTable datasource = GenTableDto.ToModal(dto);
        Boolean result = genTableService.updateById(datasource);
        return Result.success(result);
    }

    @GetMapping("/{id}/fields")
    public Result getFieldsByTableId(@PathVariable("id") String id) {
        LambdaQueryWrapper<GenTableField> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GenTableField::getTableId, id);
        List<GenTableField> tableFieldList = genTableFieldService.list(queryWrapper);
        return Result.success(tableFieldList);
    }


    @PostMapping("/import/{datasourceId}")
    public Result importTable(@PathVariable("datasourceId") String datasourceId, @RequestBody List<String> tableNameList) throws ServerException, SQLException, ClassNotFoundException {
        for (String tableName : tableNameList) {
            genTableService.tableImport(datasourceId, "", tableName);
        }
        return Result.success("");
    }

    @PostMapping("/{id}/sync")
    public Result importTable(@PathVariable("id") String tableId) throws ServerException, SQLException, ClassNotFoundException {
        GenTable datasource = genTableService.getById(tableId);
        return Result.success("");
    }
}
